module.exports = () => {
  return async function result(ctx, next) {
    const success = function (res) {
      const { data = {}, msg = '操作成功', code = 200 } = res || {};
      this.status = 400;
      this.body = { code, msg, data };
      return { code, msg, data };
    }.bind(ctx);

    const error = function (res) {
      const { msg = '操作失败', code = 400 } = res || {};
      this.status = 400;
      throw { code, message: msg };
    }.bind(ctx);

    ctx.success = success;
    ctx.error = error;
    await next();
  };
};
